<template>
  <rx-layout>
    <div slot="center" style>
      <rx-fit :isheader="isheader">
        <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
          <div class="table-operator">
            <rx-button alias="formCustomAdd" :butn-icon="'plus'" :butn-type="'primary'" :show-no-right="true" @click="add">{{$ti18('rxList.newBtn','新建')}}</rx-button>
            <a-dropdown v-if="editEnable && selectedRowKeys.length > 0 && removeEnable && selectedRowKeys.length > 0">
              <a-button style="margin-left: 8px"> {{$ti18('rxList.moreBtn','更多')}} <a-icon type="down"  /> </a-button>
              <a-menu slot="overlay"  >
                <a-menu-item key="0"  @click="editOne"  v-if="editEnable && selectedRowKeys.length > 0"
                             :butn-type="'primary'" :show-no-right="false">
                  <a-icon type="edit" />{{$ti18('rxList.editBtn','编辑')}}
                </a-menu-item>
                <a-menu-item key="1" @click="delByIds(selectedRowKeys)" v-if="removeEnable && selectedRowKeys.length > 0"  :butn-type="'danger'"
                             :show-no-right="false" >
                  <a-icon type="delete" />{{$ti18('rxList.delBtn','删除')}}
                </a-menu-item>
                <a-menu-item key="2" @click="execute(selectedRowKeys)" v-if="removeEnable && selectedRowKeys.length > 0"  :butn-type="'danger'"
                             :show-no-right="false" >
                  <a-icon type="import" />{{i18n('list.import','导入Excel')}}
                </a-menu-item>
                <a-menu-item key="3" @click="download(selectedRows)" v-if="removeEnable && selectedRowKeys.length > 0"  :butn-type="'danger'"
                             :show-no-right="false" >
                  <a-icon type="download" />{{i18n('list.down','下载模板')}}
                </a-menu-item>

              </a-menu>
            </a-dropdown>
          </div>
          <span class="search-btn-box" v-if="!isheader">
                <span class="search-btn" @click.stop="searchshow"><i class="iconfont iconguolv"></i>{{$ti18('rxList.leachBtn','过滤')}}</span>
          </span>
        </div>
        <div btnalign="right"  slot="searchbar"   v-if="fitSearch" @search="search" @cancel="cancel" @close="closeSearchbar">
          <a-form :layout="colLayout">
            <a-row :gutter="24">
              <a-col :span="colSpan">
                <a-form-item :label="i18n('form.name','模板名称')">
                  <a-input :placeholder="i18n('form.name','模板名称','input')" v-model="queryParam.Q_NAME__S_LK"/>
                </a-form-item>
              </a-col>
              <a-col :span="colSpan">
                <a-form-item :label="i18n('form.key','模板别名')">
                  <a-input :placeholder="i18n('form.key','模板别名','input')" v-model="queryParam.Q_KEY__S_LK"/>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <rx-grid
          ref="table"
          :allowRowSelect="true"
          :multiSelect="true"
          :columns="columns"
          :defaultPageSize="10"
          url="/api-system/system/core/importExcel/query"
          :queryParam="queryParam"
          data-field="result.data"
          :pageSizeOptions="['10','30','40']"
          id-field="id"
          @selectChange="onSelectChange"
        >
          <span slot="action" slot-scope="{ text, record }" class="actionIcons">
            <span v-if="editEnable && showBtn(false)"  @click="edit(record)">{{$ti18('rxList.editBtn','编辑')}}</span>
            <span v-if="removeEnable && showBtn(false)" @click="delByIds([record.id])">{{$ti18('rxList.delBtn','删除')}}</span>
            <span v-if="editEnable && showBtn()"  @click="execute([record.id])">{{i18n('list.import','导入Excel')}}</span>
            <span v-if="showBtn()" @click="showLog(record.id)">{{i18n('list.showLog','查看日志')}}</span>
          </span>
        </rx-grid>
        <a-modal class="ImportAmodal" v-model="logVisible" :title="i18n('list.importExcelLog','导入Excel日志')" :width="800" @ok="logOk">
            <a-button type="primary" style="margin-bottom:10px" @click="clearLog">{{i18n('list.clearLog','清空日志')}}</a-button>
            <rx-grid
                ref="logTable"
                :allowRowSelect="false"
                :columns="logColumns"
                :dataSource="logDataSource"
                :heightauto="true"
            >
                <span slot="log" slot-scope="{ text, record }">
                    {{text?text.length>30?text.substring(0,30)+'...':text:''}}
                </span>
                <span slot="createBy" slot-scope="{ text, record }">
                    <rx-user-info :userId="text"></rx-user-info>
                </span>
                <span slot="action" slot-scope="{ text, record }" class="actionIcons">
                    <span @click.stop="logDetail(record)">{{i18n('list.logDetail','详情')}}</span>
                  </span>
            </rx-grid>
        </a-modal>
      </rx-fit>
    </div>
  </rx-layout>
</template>

<script>
  import ImportExcelApi from '@/api/system/core/importExcel'
  import ImportExcelLogApi from "@/api/system/core/importExcelLog";
  import ImportExcelEdit from './ImportExcelEdit'
  import ImportExcelExecute from './ImportExcelExecute'
  import ImportExcelLog from "./ImportExcelLog";
  import {BaseList,Util,RxUserInfo,TokeUtil} from "@lowcode/jpaas-common-lib";
  export default {
    name: 'ImportExcelList',
    mixins: [BaseList],
    components: {
        RxUserInfo
    },
    data() {
      return {
          columns: [
              {
                  title: this.$ti18('rxList.index','序号'),
                  type: 'indexColumn',
                  width: 60,
                  dataIndex: 'serial',
                  scopedSlots: {customRender: 'serial'}
              },
              {title: this.i18n('form.name','模板名称'), dataIndex: 'name', width: 100},
              {title: this.i18n('form.key','模板别名'), dataIndex: 'key', width: 100},
              {title: this.i18n('form.remarks','备注'), dataIndex: 'comment', width: 100},
              {title: this.$ti18('rxList.action','操作'), width: 100, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
          ],
          component: ImportExcelEdit,
          comment: this.i18n('list.comment','EXCEL导入'),
          max: true,
          logVisible: false,
          logColumns: [
              {
                  title: this.$ti18('rxList.index','序号'),
                  type: 'indexColumn',
                  width: 100,
                  dataIndex: 'serial',
                  scopedSlots: {customRender: 'serial'}
              },
              {title: this.i18n('form.content','日志内容'), dataIndex: 'log', width: 200,scopedSlots: {customRender: 'log'}},
              {title: this.i18n('list.operator','操作人'), dataIndex: 'createBy', width: 100,scopedSlots: {customRender: 'createBy'}},
              {title: this.i18n('list.operatorTime','操作时间'), dataIndex: 'createTime', width: 100},
              {title: this.$ti18('rxList.action','操作'), dataIndex: 'action', width: 100,scopedSlots: {customRender: 'action'}}
          ],
          logDataSource: []
      }
    },
    methods:{
      i18n(name,text,key){
          return this.$ti18(name,text,"ImportExcel",key);
      },
      delById(ids){
        return ImportExcelApi.del({ ids: ids.join(',') });
      },
      getListByParams(parameter){
        return ImportExcelApi.query(parameter)
      },
      search(){
        this.$refs.table.loadData();
      },
      cancel(){
        this.resetSearch();
      },
      closeSearchbar(){
        //关闭过滤弹窗；（点击X触发）
        this.fitSearch = false ;
      },
      showLog(id) {
          var self=this;
          self.logTemplateId=id;
          ImportExcelLogApi.getByTemplateId(id).then(res => {
              self.logDataSource=res.data;
              self.logVisible = true;
          })
      },
      logOk(){
        this.logVisible=false;
      },
        logDetail(record){
            Util.open({
                component: ImportExcelLog,
                curVm:this,
                widthHeight:['800px','600px'],
                title: this.i18n('list.importLogDetail','导入Excel日志详情'),
                data:{
                    pkId:record.pkId
                }
            },function () {})
        },
        clearLog(){
          var self=this;
            ImportExcelLogApi.delByTemplateId(self.logTemplateId).then(res=>{
                self.showLog(self.logTemplateId);
            })
        },
      execute(ids){
        if(ids.length > 1) {
          this.$warning({
            title: this.$ti18('rxMsa.actionTips','操作提示'),
            content: this.i18n('msa.chooseTips','只能选择一个'),
          });
          return;
        }
        ImportExcelApi.get(ids).then(res => {
          let data = res.data;
          Util.open(
            {
              component: ImportExcelExecute,
              curVm: this,
              widthHeight: ['600px', '400px'],
              title: this.i18n('list.import','导入Excel'),
              data: {
                name: data.name,
                gridData: data.gridData,
                headerContent: data.headerContent,
                pkId : ids,
              }
            }, function (action) {
            });

        })

      },

      download(ids){
        if(ids.length > 1){
          this.$warning({
            title: this.$ti18('rxMsa.actionTips','操作提示'),
            content: this.i18n('msa.chooseTips','只能选择一个'),
          });
          return;
        }

        var accessToken = TokeUtil.getToken();
        const url = '/api/api-system/system/core/sysFile/download/'+ ids[0].templateId+'?accessToken='+accessToken;
        window.open(url);
        return ;
      }
    }
  }
</script>
<style scoped>

  .ImportAmodal>>>.ant-modal-body{
      padding:10px!important;
  }
  .ImportAmodal>>>.ant-modal-footer >div{
      text-align: center;
  }
</style>

